<?php
use Phalcon\Paginator\Adapter\Model as PaginatorModel;
class IndexController extends ControllerBase
{

    public function indexAction()

    {
        $curPage=empty($_GET['page'])?1:$_GET['page']*1;
        //查询数据 并转换成数组
     $users=User::find(['order'=>'id desc']);
       $page=new PaginatorModel(array(
           'data'=>$users,
           'limit'=>10,
           'page'=>$curPage,
       ));
        $res=$page->getPaginate();
     $this->view->setVar('data',$res);
      if($this->request->isAjax()){
          return $this->response->setJsonContent($res->items);
      }
    }

    //存储
    public function storeAction(){

    }

    public function createAction(){

        //如果为post提交
        if($this->request->isPost()) {
            $data = $this->request->getPost();
            $user_model = new User();
            //获取当前验证的值
            $form = new UserForm();
            if ($form->isValid($data) && $user_model->save($data)) {
                $this->flashSession->success('新增成功');
                return $this->response->redirect('index/store');
            } else {
                //获取当前验证的值
                $mes = [];
                foreach ($form->getMessages() as $message) {
                    $mes[$message->getField()]['field'] = $message->getField();
                    $mes[$message->getField()]['mes'] = $message->getMessage();
                }
            }
        }
        $this->view->setVars($mes);
        $this->view->partial('index/store');
    }

    //删除
    public function deleteAction(){
        if($this->request->isAjax()){
            $id=$this->request->getPost('id');
            //获取这条数据
            $user=User::findFirst($id);
            if($user){//存在则删除
                if($user->delete()){
                    echo $id;
                }else{
                    echo  0;
                }
            }
            //停止页面输出
            return $this->view->disable();
        }
    }

    //修改
    public function editAction(){


            $id = $this->request->get('id') * 1;

            $row = User::findFirst(['conditions' => "id={$id}", 'columns' => 'userName,emai']);

            if (!$row) {
                return $this->response->redirect('index/index'); //不存在返回首页
            }
            $this->tag->setDefault('id', $id);
            $this->tag->setDefault('userName', $row->userName);//回显
            $this->tag->setDefault('emai', $row->emai);

            $mes=$this->session->get('error');
            if(empty($mes)){ //如果验证信息为空
                $mes=[];
            }
            $this->view->setVar('cur_url',$this->request->getURI() ); //当前url地址
            $this->view->setVar('mes',$mes); //验证失败信息
            $this->session->set('error','');
            $this->view->partial('index/edit');
    }

    public function updateAction(){

        if($this->request->isPost()){
            $id=$this->request->getPost('id');
            $user_model=new User();
            $user_model->id=$id;
            $user_model->userName=$this->request->getPost('userName');
            $user_model->emai=$this->request->getPost('emai');
            if($user_model->update()){
                 $this->flashSession->success('修改成功');
                return $this->response->redirect('index/index');
            }else{
                $this->flashSession->error('修改失败');
                //获取当前验证的值
                $mes = [];
                foreach ($user_model->getMessages() as $message) {
                    $mes[$message->getField()]['field'] = $message->getField();
                    $mes[$message->getField()]['mes'] = $message->getMessage();
                }
                $this->session->set('error',$mes);

                return $this->dispatcher->forward([
                    'controller'=>'index',
                    'action'=>'edit',
                    'params'=>['id'=>$id]
                ]);

            }

        }
        $this->view->disable();

    }

}

